package com.example.bes012.multiple.ds.demo.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages="com.example.bes012.multiple.ds.demo.mapper.datasource2", sqlSessionFactoryRef = "sqlSessionFactoryTest2")
public class MybatisConfig2 {

    @Autowired
    @Qualifier(value = "dataSource2")
    private DataSource dataSource2;

    @Bean(value = "sqlSessionFactoryTest2")
    public SqlSessionFactory sqlSessionFactoryTest2() throws Exception {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setDataSource(dataSource2);
        //指定mapper位置
        factory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/d2/*.xml"));
        return factory.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplateTest2() throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryTest2());
        return template;
    }

}
